//
//  UserGuidanceViewController.m
//  FamilyHouse
//
//  Created by bjwang on 15-4-21.
//  Copyright (c) 2015年 BeiJingShiJianChuanMei. All rights reserved.
//

#import "UserGuidanceViewController.h"
#import "MLTabBar.h"
#import "AppDelegate.h"
#import "RecommendViewController.h"
#import "LoginViewController.h"
#import "RegisterViewController.h"


#define  kRatio (DeviceWidth/320.0)
//  #import <AudioToolbox/AudioToolbox.h>
//static SystemSoundID nan_sound_male_id = 0;
//static SystemSoundID nv_sound_male_id = 1;

@interface UserGuidanceViewController ()<UIScrollViewDelegate>
{
  //  UIButton *nansheng;
    //UIButton *nvsheng;
//    id homeVc;
}
//@property (nonatomic)BOOL is;//yes正在播放,请稍后

@property (nonatomic, strong)UIImageView *hintImgView;
@end

@implementation UserGuidanceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   // self.is = YES;
    [self setupScrollView];
    [self setupPageControl];
    // Do any additional setup after loading the view.
}

- (void)foundhintImgView
{
    //新增
    self.hintImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, DeviceWidth, DeviceHeight)];
    self.hintImgView.userInteractionEnabled = YES;
 //   self.hintImgView.contentMode = UIViewContentModeScaleAspectFit;
    self.hintImgView.image = [UIImage imageNamed:@"背景图"];
//    [[UIApplication sharedApplication].delegate.window addSubview:self.hintImgView];
    
    
    UIImageView *imgV = [[UIImageView alloc] initWithFrame:CGRectMake(DeviceWidth/2 - 152/2 , DeviceHeight/2 - 51/2 - 50, 152, 51)];
    imgV.image = [UIImage imageNamed:@"闪屏-文字"];
    [self.hintImgView addSubview:imgV];
    
    
    UIButton *strollBt = [UIButton buttonWithType:UIButtonTypeCustom];
    strollBt.frame = CGRectMake(20, DeviceHeight - 90, DeviceWidth - 40, 30);
    [strollBt setTitle:@"先逛逛" forState:UIControlStateNormal];
    [strollBt setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [strollBt setTitleColor:RGBA(188, 188, 188, 1) forState:UIControlStateHighlighted];
    strollBt.titleLabel.font = kFont(13);
    [strollBt addTarget:self action:@selector(clickstrollBt) forControlEvents:UIControlEventTouchUpInside];
    strollBt.layer.masksToBounds = YES;
    strollBt.layer.cornerRadius = 15;
    strollBt.layer.borderColor = [UIColor whiteColor].CGColor;
    strollBt.layer.borderWidth = 1;
    [_hintImgView addSubview:strollBt];
    
    CGFloat w =( DeviceWidth - 20 -20 -20 )/2;
    
    UIButton *loginBt = [UIButton buttonWithType:UIButtonTypeCustom];
    loginBt.frame = CGRectMake(20, DeviceHeight - 50, w, 40);
    [loginBt setTitle:@"登录" forState:UIControlStateNormal];
    loginBt.backgroundColor = RGBA(34, 34, 50, 1);
    [loginBt setTitleColor:RGBA(255, 255, 255, 0.5) forState:UIControlStateNormal];
    [loginBt setTitleColor:RGBA(188, 188, 188, 1) forState:UIControlStateHighlighted];
    loginBt.titleLabel.font = kFont(13);
    [loginBt addTarget:self action:@selector(clickloginBt) forControlEvents:UIControlEventTouchUpInside];
    loginBt.layer.masksToBounds = YES;
    loginBt.layer.cornerRadius = 20;
    
    [_hintImgView addSubview:loginBt];
    
    
    UIButton *rBt = [UIButton buttonWithType:UIButtonTypeCustom];
    rBt.frame = CGRectMake(20+w+20, DeviceHeight - 50, w, 40);
    [rBt setTitle:@"注册" forState:UIControlStateNormal];
    rBt.backgroundColor = RGBA(120, 120, 120, 1);
    [rBt setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [rBt setTitleColor:RGBA(188, 188, 188, 1) forState:UIControlStateHighlighted];
    rBt.titleLabel.font = kFont(13);
    [rBt addTarget:self action:@selector(clickrBt) forControlEvents:UIControlEventTouchUpInside];
    rBt.layer.masksToBounds = YES;
    rBt.layer.cornerRadius = 20;
    
    [_hintImgView addSubview:rBt];
}
//先逛逛
- (void)clickstrollBt
{
   //  [homeVc clickstrollBt];
    RecommendViewController *reVC = [[RecommendViewController alloc] init];
    reVC.special = YES;
    UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:reVC];
    
    [self presentViewController:naVC animated:YES completion:^{
        
        
    }];
}

//注册
- (void)clickrBt
{
    RegisterViewController *registerVC = [[RegisterViewController alloc] init];
    UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:registerVC];
    registerVC.hidesBottomBarWhenPushed = YES;
    [self presentViewController:naVC animated:YES completion:^{
        
    }];
    
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
   // AudioServicesDisposeSystemSoundID(nan_sound_male_id);
    //AudioServicesDisposeSystemSoundID(nv_sound_male_id);
}
//-(instancetype)initWithVC:(id)vc
//{
//    self = [super init];
//    if (self) {
//        homeVc =vc;
//    }
//    return self;
//}

//- (UIImageView *)myView
//{
//    UIImageView *backImageView = [[UIImageView alloc] init];
//    backImageView.userInteractionEnabled = YES;
//     [backImageView setImage:[UIImage imageNamed:@""]];
//    UIImageView *dialog = [[UIImageView alloc] initWithFrame:CGRectMake((DeviceWidth-254*kRatio)/2, 128*kRatio, 254*kRatio, 227*kRatio)];
//    dialog.userInteractionEnabled = YES;
//    
//    [dialog setImage:ReadImageFromBundle(@"walkthrough04_dialog", @"png")];
//    UIImageView *title = [[UIImageView alloc] initWithFrame:CGRectMake((dialog.frame.size.width-118*kRatio)/2, 19*kRatio, 118*kRatio, 17*kRatio)];
//     [title setImage:ReadImageFromBundle(@"walkthrough04_title", @"png")];
//    [dialog addSubview:title];
//    
//    
//    
//    UIImageView *head_girl = [[UIImageView alloc] initWithFrame:CGRectMake(17*kRatio, 75*kRatio, 53*kRatio, 58*kRatio)];
//    [head_girl setImage:ReadImageFromBundle(@"walkthrough04_head_girl@2x", @"png")];
//    [dialog addSubview:head_girl];
//    
//    nvsheng = [UIButton buttonWithType:UIButtonTypeCustom];
//    nvsheng.frame = CGRectMake(head_girl.frame.origin.x+head_girl.frame.size.width+3*kRatio, head_girl.frame.origin.y+5*kRatio, 161*kRatio, 54*kRatio);
//    
//    UIImageView *chat_red = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, nvsheng.frame.size.width, nvsheng.frame.size.height)];
//    chat_red.image = ReadImageFromBundle(@"walkthrough04_chat_red", @"png");
//    [nvsheng addSubview:chat_red];
//    [chat_red setContentMode:UIViewContentModeScaleAspectFill];
//    [nvsheng addTarget:self action:@selector(clickNv) forControlEvents:UIControlEventTouchUpInside];
//    [dialog addSubview:nvsheng];
//    
//    UIView *line = [[UIView alloc] init];
//     if (IS_IPHONE_6_PLUS) {
//         line.frame = CGRectMake(58*kRatio, (54/2-8)*kRatio, 85*kRatio, 6*kRatio);
//     }else{
//         line.frame = CGRectMake(58*kRatio, (54/2-8)*kRatio, 85*kRatio, 6*kRatio);
//     }
//    line.layer.cornerRadius = 3*kRatio;
//    
//    UIView *colour = [[UIView alloc] init];
//    colour.tag = 112;
//    colour.frame = CGRectMake(0, 0, 0, 6*kRatio);
//    colour.layer.cornerRadius = 3*kRatio;
//    line.backgroundColor = RGBA(215, 227, 225, 1);
//     colour.backgroundColor = APPCOLOR(1);
//    [line addSubview:colour];
//    [nvsheng addSubview:line];
//    
//    UIImageView *head_man = [[UIImageView alloc] init];
//    CGRect manFrame = head_girl.frame;
//    manFrame.origin.y = manFrame.origin.y+manFrame.size.height +18;
//    head_man.frame = manFrame;
//    [head_man setImage:ReadImageFromBundle(@"walkthrough04_head_man", @"png")];
//    [dialog addSubview:head_man];
//    nansheng = [UIButton buttonWithType:UIButtonTypeCustom];
//    CGRect blueFrame = nvsheng.frame;
//    blueFrame.origin.y = blueFrame.origin.y+blueFrame.size.height +18;
//    nansheng.frame = blueFrame;
//    
//    UIImageView *chat_blue = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, nansheng.frame.size.width, nansheng.frame.size.height)];
//    chat_blue.image = ReadImageFromBundle(@"walkthrough04_chat_blue", @"png");
//    [nansheng addSubview:chat_blue];
//    [chat_blue setContentMode:UIViewContentModeScaleAspectFill];
//    [nansheng addTarget:self action:@selector(clickNan) forControlEvents:UIControlEventTouchUpInside];
//    [dialog addSubview:nansheng];
//    
//    UIView *lineM = [[UIView alloc] init];
//    if (IS_IPHONE_6_PLUS) {
//        lineM.frame = CGRectMake(58*kRatio, (54/2-8)*kRatio, 85*kRatio, 6*kRatio);
//    }else{
//        lineM.frame = CGRectMake(58*kRatio, (54/2-8)*kRatio, 85*kRatio, 6*kRatio);
//    }
//    lineM.layer.cornerRadius = 3*kRatio;
//    
//    UIView *colourM = [[UIView alloc] init];
//    colourM.tag = 113;
//    colourM.frame = CGRectMake(0, 0, 0, 6*kRatio);
//    colourM.layer.cornerRadius = 3*kRatio;
//    lineM.backgroundColor = RGBA(215, 227, 225, 1);
//    colourM.backgroundColor = RGBA(97, 207, 218, 1);
//    [lineM addSubview:colourM];
//    [nansheng addSubview:lineM];
//    
//    [backImageView addSubview:dialog];
//    return  backImageView;
//}




//创建程序第一次加载要显示的视图
- (void)setupScrollView
{
    CGRect r = [[UIScreen mainScreen] applicationFrame];
    UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    scrollView.delegate =self;
    [self.view addSubview:scrollView];
    //关闭水平方向上的滚动条
    scrollView.showsHorizontalScrollIndicator =NO;
    //是否可以整屏滑动
    scrollView.pagingEnabled =YES;
    scrollView.bounces = NO;
    scrollView.tag =200;
    scrollView.contentSize =CGSizeMake(r.size.width *4, [UIScreen mainScreen].bounds.size.height);
    for (int i = 0; i < 4; i++) {
         UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(r.size.width * i,0,r.size.width, [UIScreen mainScreen].bounds.size.height)];
        if(i <3){
            
           
            
            NSString *str;
            if (IS_IPHONE_4) {
                str = [NSString stringWithFormat:@"walkthrough0%d_4@2x", i+1];
            }else if(IS_IPHONE_5){
                str =[NSString stringWithFormat:@"walkthrough0%d_5@2x", i+1];
            }else{
                str =[NSString stringWithFormat:@"walkthrough0%d_6@2x", i+1];
            }
            imageView.image =ReadImageFromBundle(str, @"png");
//            if (i == 3) {
//                imageView.userInteractionEnabled = YES;
//                UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:homeVc action:@selector(clickBt)];
//                [imageView addGestureRecognizer:tapGR];
//            }
          
        }else{
            //
             imageView.userInteractionEnabled = YES;
            [self foundhintImgView];
            [imageView addSubview:self.hintImgView];
            
        }
        
          [scrollView addSubview:imageView];
      
        
       
        
    }
    
    
    
   
//    UIImageView *imageview = [self myView];
//    imageview.frame = CGRectMake(r.size.width*3, 0, DeviceWidth, DeviceHeight);
//    [scrollView addSubview:imageview];
    
//    UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
//    // button.backgroundColor=[UIColor darkGrayColor];
//    // [button setTitle:@"开始体验" forState:UIControlStateNormal];
////    [button setImage:[UIImage imageNamed:@"walkthrough04_button"] forState:UIControlStateNormal];
//    [button setBackgroundImage:ReadImageFromBundle(@"walkthrough04_button", @"png") forState:UIControlStateNormal];
//    [button setTitleColor: [UIColor grayColor] forState:UIControlStateHighlighted];
//    button.frame=CGRectMake(r.size.width*3+r.size.width/2-52, [UIScreen mainScreen].bounds.size.height -80, 104, 32);
//    [button addTarget:homeVc action:@selector(clickBt) forControlEvents:UIControlEventTouchUpInside];
//    [scrollView addSubview:button];
}
- (void)clickBt
{
    
}
//消失并登录
- (void)clickloginBt
{
   // [homeVc clickBt];
    
    LoginViewController *login = [[LoginViewController alloc]init];
    //    login.modalPresentationStyle = UIModalPresentationFormSheet;
    UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:login];
    login.hidesBottomBarWhenPushed = YES;
    [self presentViewController:naVC animated:YES completion:^{
        
    }];
    
    
}
//- (void)clickNv
//{
//    if(self.is){
//        self.is = NO;
//        AudioServicesDisposeSystemSoundID(nan_sound_male_id);
//        AudioServicesDisposeSystemSoundID(nv_sound_male_id);
//        NSString *path = [[NSBundle mainBundle] pathForResource:@"nvsheng" ofType:@"wav"];
//        if (path) {
//            //注册声音到系统
//            AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:path],&nv_sound_male_id);
//            AudioServicesPlaySystemSound(nv_sound_male_id);
//            //        AudioServicesPlaySystemSound(shake_sound_male_id);//如果无法再下面播放，可以尝试在此播放
//        }
//        
//        AudioServicesPlaySystemSound(nv_sound_male_id);
//          [self setmyis];
//        UIView *Line = (UIView *)[self.view viewWithTag:112];
//        [Utils animationMobileTimer:17 View:Line Rect:CGRectMake(0, 0, 85*kRatio, 6*kRatio)];
//    }else{
//       [HUD Prompt:@"正在播放,请稍后"];
//         AudioServicesDisposeSystemSoundID(nv_sound_male_id);
//    }
//    
//}
//-(void) playSound:(NSString *)str
//
//{
//    NSString *path = [[NSBundle mainBundle] pathForResource:@"nvsheng" ofType:@"wav"];
//    if (path) {
//        //注册声音到系统
//        AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:path],&nv_sound_male_id);
//        AudioServicesPlaySystemSound(nv_sound_male_id);
//        //        AudioServicesPlaySystemSound(shake_sound_male_id);//如果无法再下面播放，可以尝试在此播放
//    }
//    
//    AudioServicesPlaySystemSound(nv_sound_male_id);   //播放注册的声音，（此句代码，可以在本类中的任意位置调用，不限于本方法中）
//    
//    //    AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);   //让手机震动
//}

//- (void)clickNan
//{
//    if(self.is){
//        self.is = NO;
//         AudioServicesDisposeSystemSoundID(nan_sound_male_id);
//         AudioServicesDisposeSystemSoundID(nv_sound_male_id);
//       //  [Utils playWavName:@"nansheng"];
//        NSString *path = [[NSBundle mainBundle] pathForResource:@"nansheng" ofType:@"wav"];
//        if (path) {
//            //注册声音到系统
//            AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:path],&nan_sound_male_id);
//            AudioServicesPlaySystemSound(nan_sound_male_id);
//            //        AudioServicesPlaySystemSound(shake_sound_male_id);//如果无法再下面播放，可以尝试在此播放
//        }
//        
//        AudioServicesPlaySystemSound(nan_sound_male_id);
//        [self setmyis];
//        UIView *Line = (UIView *)[self.view viewWithTag:113];
//        [Utils animationMobileTimer:17 View:Line Rect:CGRectMake(0, 0, 85*kRatio, 6*kRatio)];
//    }else{
//         [HUD Prompt:@"正在播放,请稍后"];
//        
//    }
//}
//- (void)setmyis
//{
//    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(17 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//        self.is = YES;
//        UIView *LineM = (UIView *)[self.view viewWithTag:113];
//        UIView *Line = (UIView *)[self.view viewWithTag:112];
//        Line.frame = CGRectMake(0, 0, 0, 6*kRatio);
//        LineM.frame = CGRectMake(0, 0, 0, 6*kRatio);
//    });
//}
- (void)setupPageControl
{
    UIPageControl *pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height -40, [UIScreen mainScreen].bounds.size.width, 20)];
    pageControl.tag =100;
    //设置表示的页数
    pageControl.numberOfPages =4;
    //设置选中的页数
    pageControl.currentPage =0;
    //设置未选中点的颜色
    pageControl.pageIndicatorTintColor = [UIColor whiteColor];
    //设置选中点的颜色
    pageControl.currentPageIndicatorTintColor = SelectViewClolor;
    //添加响应事件
    [pageControl addTarget:self action:@selector(handlePageControl:)forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:pageControl];
    
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

{
    UIPageControl *pagControl = (UIPageControl *)[self.view viewWithTag:100];
    pagControl.currentPage = scrollView.contentOffset.x / [UIScreen mainScreen].bounds.size.width;
    if(pagControl.currentPage == 3){
        pagControl.hidden = YES;
    }else{
        pagControl.hidden = NO;
    }
    
}

- (void)handlePageControl:(UIPageControl *)pageControl

{
    //切换pageControl .对应切换scrollView不同的界面
    UIScrollView *scrollView = (UIScrollView *)[self.view viewWithTag:200];
    //
    [scrollView setContentOffset:CGPointMake(DeviceWidth * pageControl.currentPage,0)animated:YES];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    if ([self isViewLoaded]&&!self.view.window) {
        self.view = nil;
    }
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
